package com.hrabun.goodmorningalarm;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MainActivity extends Activity implements OnClickListener {
	
	private static Context context;
	
	/* Variables */
	ArrayList<Map<String, Object>> listData;
	Map<String, Object> listMap;
	ListView listAlarms;
	Button btnAddAlarm;
	Database myDatabase;
	ArrayList<AlarmItem> aItems = new ArrayList<AlarmItem>();
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getApplicationContext();
        this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        setContentView(R.layout.activity_main);

        registerControls();
        setFonts();
        registerListeners();
        refreshList();
        
        
    }
        
    public static Context getAppContext() {
    	return context;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
		case R.id.itemClearAll:
		{
			deleteAllAlarms();
			refreshList();
		}
			break;
		case R.id.menu_settings: 
		{
			Intent intent = new Intent(this, PrefActivity.class);
			startActivity(intent);
		}	break;
		default:
			break;
		}
    	return super.onOptionsItemSelected(item);
    }
    
    private void deleteAllAlarms() {
    	for (AlarmItem aItem : aItems) {
    		aItem.deleteAlarmItem();
		}

    }
    
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnAddAlarm:
		{
			addNewAlarm();
		}
			break;

		default:
			break;
		}
		
	}
	
	private void registerControls() {
		listAlarms = (ListView) findViewById(R.id.listAlarms);
		btnAddAlarm = (Button) findViewById(R.id.btnAddAlarm);
	}
	
	private void setFonts() {
		btnAddAlarm.setTypeface(Global.setFontRibeye());
	}
	
	private void registerListeners() {
		btnAddAlarm.setOnClickListener(this);
		
		listAlarms.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	private void refreshList() {
		aItems.clear();
		listData = new ArrayList<Map<String,Object>>();
		Database db = new Database(this);
		aItems = db.getAllAlarms();
		
		for (int i = 0; i < aItems.size(); i++) {
			listMap = new HashMap<String, Object>();
			listMap.put(Global.DATA_ID_ATTR, aItems.get(i).id);
			listMap.put(Global.DATA_TIME_ATTR, Global.getTimeFromDate(aItems.get(i).alarmTime));
			listMap.put(Global.DATA_ACTIVE_ATTR, aItems.get(i).active);
			listData.add(listMap);
		}
		
		String[] from = {Global.DATA_TIME_ATTR, Global.DATA_ACTIVE_ATTR};
		int[] to = {R.id.txtAlarmItemTime, R.id.btnAlarmItemToggle};
		ListAdapter adapter = new AlertsListAdapter(this, listData, R.layout.alarm_item, from, to);
		listAlarms = (ListView) findViewById(R.id.listAlarms);
		listAlarms.setAdapter(adapter);
		registerForContextMenu(listAlarms);
		

	}

	private void addNewAlarm() {
		Intent intent = new Intent(this, AddNewAlarm.class);
		intent.putExtra("mode", Global.DIALOG_TIME_ADD);
		startActivityForResult(intent, Global.DIALOG_TIME_ADD);
	}
	
	public void deleteAlarmItem(AlarmItem aItem) {
		
	}
	
	public void changeAlarmItem(AlarmItem aItem) {
		Intent intent = new Intent(this, AddNewAlarm.class);
		intent.putExtra("mode", Global.DIALOG_TIME_CHANGE);
		intent.putExtra("id", aItem.id);
		startActivityForResult(intent, Global.DIALOG_TIME_CHANGE);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case Global.DIALOG_TIME_ADD:
		{
			if (resultCode == RESULT_OK) {
					Calendar cal = Calendar.getInstance();
					cal.set(Calendar.HOUR_OF_DAY, data.getIntExtra("hours", 0));
					cal.set(Calendar.MINUTE, data.getIntExtra("mins", 0));
					cal.set(Calendar.SECOND, 0);
					AlarmItem aitem = new AlarmItem("test", cal.getTime(), 
							data.getBooleanExtra("mon", false), 
							data.getBooleanExtra("tue", false), 
							data.getBooleanExtra("wed", false), 
							data.getBooleanExtra("th", false), 
							data.getBooleanExtra("fri", false),
							data.getBooleanExtra("sat", false), 
							data.getBooleanExtra("sun", false), 
							true);
					aitem.setRadioAssigned(String.valueOf(data.getLongExtra("radio",1)));
					aitem.alarmAdd();
				
			}
		}
			break;
		case Global.DIALOG_TIME_CHANGE:
		{
			if (resultCode == RESULT_OK) {
				Calendar cal = Calendar.getInstance();
				cal.set(Calendar.HOUR_OF_DAY, data.getIntExtra("hours", 0));
				cal.set(Calendar.MINUTE, data.getIntExtra("mins", 0));
				cal.set(Calendar.SECOND, 0);
				AlarmItem aItem = new AlarmItem();
				aItem = aItem.getAlarmItemById(data.getExtras().getString("id"));
				
				aItem.setAlarmTime(cal.getTime());
				aItem.setMonday(data.getBooleanExtra("mon", false));
				aItem.setTuesday(data.getBooleanExtra("tue", false));
				aItem.setWednesday(data.getBooleanExtra("wed", false));
				aItem.setThursday(data.getBooleanExtra("th", false));
				aItem.setFriday(data.getBooleanExtra("fri", false));
				aItem.setSaturday(data.getBooleanExtra("sat", false));
				aItem.setSunday(data.getBooleanExtra("sun", false));
				aItem.setRadioAssigned(String.valueOf(data.getLongExtra("radio",1)));
				aItem.updateAlarmItem();
			}
		}
		
		default:
			break;
		}
		refreshList();
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		refreshList();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.list_context_menu, menu);
	}
	
	@SuppressWarnings("unused")
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.itemDelete:
		{
			AdapterView.AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
			AlarmItem test = (AlarmItem) ((ViewHolder) info.targetView.getTag()).btnActive.getTag();
			test.deleteAlarmItem();
			refreshList();
		}	break;

		default:
			break;
		}
		return super.onContextItemSelected(item);
	}
}
